package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.ZyBuilding;
import com.blb.zy_community.entity.ZyCommunity;
import com.blb.zy_community.entity.ZyUnit;
import com.blb.zy_community.service.IZyBuildingService;
import com.blb.zy_community.service.IZyCommunityService;
import com.blb.zy_community.service.impl.ZyBuildingServiceImpl;
import com.blb.zy_community.service.impl.ZyUnitServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 单元  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@CrossOrigin
@RestController
public class ZyUnitController {
    @Autowired
    private IZyBuildingService zyBuildingService;
    @Autowired
    private IZyCommunityService zyCommunityService;
    @Autowired
    private ZyUnitServiceImpl zyUnitService;
    @GetMapping("/unit-page")
    public ResponseResult<Page<ZyUnit>> getByUnitPage(@RequestParam(required = false, defaultValue = "1") Long current,
                                                      @RequestParam(required = false, defaultValue = "5") Long size,
                                                      @RequestParam(required = false, defaultValue = "") String unitName,
                                                      @RequestParam(required = false, defaultValue = "") String unitCode,
                                                      @RequestParam(required = false, defaultValue = "") String unitHaveElevator,
                                                      @RequestParam(required = false, defaultValue = "") String buildingId,
                                                      @RequestParam(required = false, defaultValue = "") Long communityId) {
        Page<ZyUnit> byUnit = zyUnitService.getByUnit(current, size, unitName, unitCode, unitHaveElevator, buildingId,communityId);
        return ResponseResult.ok(byUnit);

    }

    @PostMapping("/unit")
    public ResponseResult<String> addUnit(@RequestBody ZyUnit zyUnit) {
        zyUnitService.save(zyUnit);
        return ResponseResult.ok(zyUnit);
    }

    @DeleteMapping("/unit/{unitId}")
    public ResponseResult<String> deleteUnit(@PathVariable Long unitId) {
        zyUnitService.removeById(unitId);
        return ResponseResult.ok(unitId);
    }

    @PutMapping("/unit")
    public ResponseResult<String> updateUnit(@RequestBody ZyUnit zyUnit) {
        zyUnitService.updateById(zyUnit);
        return ResponseResult.ok(zyUnit);
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @DeleteMapping("/units/{unitIds}")
    public ResponseResult<String> deleteUnits(@PathVariable Long[] unitIds) {
        zyUnitService.deleteUnit(unitIds);
        return ResponseResult.ok(unitIds);
    }
    @RequestMapping("/unit/{id}")
    public ResponseResult<String> getOneUnit(@PathVariable Long id){
        ZyUnit unitId = zyUnitService.getOne(new QueryWrapper<ZyUnit>().eq("unit_id", id));
        return ResponseResult.ok(unitId);
    }

    @RequestMapping("communityList")
    public ResponseResult<String> communityList() {
        List<ZyCommunity> list = zyCommunityService.list();
        return ResponseResult.ok(list);
    }

    @RequestMapping("buildingList")
    public ResponseResult<String> buildingList() {
        List<ZyBuilding> list = zyBuildingService.list();
        return ResponseResult.ok(list);
    }

    /**
     * 根据楼栋信息查单元
     * @param id
     * @return
     */
    @GetMapping("buildingByUnit")
    public ResponseResult<String> buildingByUnitById(@RequestParam Long id){
        List<ZyUnit> buildingId = zyUnitService.list(new QueryWrapper<ZyUnit>().eq("building_id", id));
        return ResponseResult.ok(buildingId);
    }

}
